#include "tcpsocket.hpp"
#include"cala.hpp"
#include<string.h>
#include<signal.h>

#define CHECK_RET(r) if((r) == false){return -1;}
int main(int argc,char* argv[])
{
    if(argc!=3){
        std::cout<<"usage:"<<"./tcp_cli 172.18.0.1 9000\n";
        return -1;
    }
    signal(SIGPIPE,SIG_IGN);
    std::string ip = argv[1];
    uint16_t port = std::stoi(argv[2]);
    Tcpsocket cli_sock;
    //1.创建套接字
    CHECK_RET(cli_sock.Socket());
    //2.绑定地址
    CHECK_RET(cli_sock.Connect(ip,port));
    while(1){
        //3.接受数据
        int num1,num2;
        char op;
        std::cout<<"input arithmetic expression: ";
        fflush(stdout);
        std::cin>>num1>>op>>num2;
        cala_t tmp(num1,num2,op);
        // int fd = cli_sock.Fd();
        // send(fd,&tmp,sizeof(cala_t),0);
        // int result;
        // recv(fd,&result,sizeof(int),0);
        // std::cout<<"result:"<<result<<std::endl;
        std::string body;
        body.assign((char*)&tmp,sizeof(cala_t));
        cli_sock.Send(body);
        cli_sock.Recv(&body);
        std::cout<<"result: "<<*(int*)&body[0]<<std::endl;
        //std::string body;
        //cli_sock.Send(body);
        //cli_sock.Recv(&body);
        //std::cout<<"serve say:"<<body<<std::endl;
    }
    //4.关闭套接字
    cli_sock.Close();
    return 0;
}